package org.ahuanlien.crm.service.impl;

import org.ahuanlien.crm.domain.BillRecord;
import org.ahuanlien.crm.domain.CheckOrder;
import org.ahuanlien.crm.domain.BackOrder;
import org.ahuanlien.crm.mapper.BillRecordMapper;
import org.ahuanlien.crm.mapper.CheckOrderMapper;
import org.ahuanlien.crm.mapper.BackOrderMapper;
import org.ahuanlien.crm.query.BackOrderQueryObject;
import org.ahuanlien.crm.service.IBackOrderService;
import org.ahuanlien.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class BackOrderServiceImpl implements IBackOrderService{
    @Autowired
    BackOrderMapper backOrderMapper;

    @Autowired
    BillRecordMapper billRecordMapper;

    @Autowired
    CheckOrderMapper checkOrderMapper;

    public int deleteByPrimaryKey(Long id) {
        BackOrder backOrder = backOrderMapper.selectByPrimaryKey(id);
        //把拒保的信息存入单据历史记录
        BillRecord billRecord = new BillRecord();
        billRecord.setSn(backOrder.getSn());
        billRecord.setHolder(backOrder.getHolder());
        billRecord.setCar(backOrder.getCarNo());
        billRecord.setADate(backOrder.getADate());
        billRecord.setPDate(backOrder.getPDate());
        billRecord.setYear(backOrder.getYear());
        billRecord.setOpUser(backOrder.getOpUser());
        billRecord.setAuditor(backOrder.getAuditor());
        billRecord.setTotalAmount(backOrder.getTotalAmount());
        billRecord.setState("拒保");
        billRecord.setItem("保险产品");
        billRecordMapper.insert(billRecord);
        return backOrderMapper.deleteByPrimaryKey(id);
    }

    public int insert(BackOrder record) {
        return backOrderMapper.insert(record);
    }

    public BackOrder selectByPrimaryKey(Long id) {
        return backOrderMapper.selectByPrimaryKey(id);
    }

    public List<BackOrder> selectAll() {
        return backOrderMapper.selectAll();
    }

    public int updateByPrimaryKey(BackOrder record) {
        return backOrderMapper.updateByPrimaryKey(record);
    }

    public PageResult query(BackOrderQueryObject qo) {
        int count = backOrderMapper.queryForCount();
        List<BackOrder> list = backOrderMapper.queryForList(qo);
        if(count== 0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        return new PageResult(count,list);
    }
    //退回申请的提审功能
    public void audit(Long id) {
        BackOrder backOrder = backOrderMapper.selectByPrimaryKey(id);
        //把信息存入审核单
        CheckOrder checkOrder = new CheckOrder();
        checkOrder.setSn(backOrder.getSn());
        checkOrder.setHolder(backOrder.getHolder());
        checkOrder.setCarNo(backOrder.getCarNo());
        checkOrder.setADate(backOrder.getADate());
        checkOrder.setYear(backOrder.getYear());
        checkOrder.setOpUser(backOrder.getOpUser());
        checkOrder.setTotalAmount(backOrder.getTotalAmount());
        checkOrder.setState(backOrder.getState());
        checkOrder.setItem("保险产品明细");
        checkOrderMapper.insert(checkOrder);
        backOrderMapper.deleteByPrimaryKey(id);

        //把数据存入单据历史记录
        BillRecord billRecord = new BillRecord();
        billRecord.setSn(backOrder.getSn());
        billRecord.setHolder(backOrder.getHolder());
        billRecord.setCar(backOrder.getCarNo());
        billRecord.setADate(backOrder.getADate());
        billRecord.setYear(backOrder.getYear());
        billRecord.setOpUser(backOrder.getOpUser());
        billRecord.setTotalAmount(backOrder.getTotalAmount());
        billRecord.setState("已修改待审核");
        billRecord.setItem("保险产品明细");
        billRecordMapper.insert(billRecord);
    }
}





